package com.lin.maker.generator.main;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.lin.maker.generator.JarGenerator;
import com.lin.maker.generator.ScripyGenerator;
import com.lin.maker.generator.file.DynamicFileGenerator;
import com.lin.maker.meta.Meta;
import com.lin.maker.meta.MetaManager;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;

public abstract class MainTemplate {
    public void doGenerator() throws TemplateException, IOException, InterruptedException{
        Meta meta = MetaManager.getMeta();
        String property = System.getProperty("user.dir");
        String outputPath = property + File.separator + "generated" + File.separator + meta.getName();
        doGenerator(meta,outputPath);
    }

    /**
     * 接收参数生成代码的方法
     * @param meta
     * @param outputPath
     * @throws TemplateException
     * @throws IOException
     * @throws InterruptedException
     */
    public void doGenerator(Meta meta,String outputPath) throws TemplateException, IOException, InterruptedException{
        if(!FileUtil.exist(outputPath)){
            FileUtil.mkdir(outputPath);
        }
        // 复制原始文件
        String sourceRootDestPath = getSourceRootDestPath(meta, outputPath);
        //生成 生成代码
        doGeneratorCode(outputPath, meta);
        //生成jar包
        getJar(outputPath, meta);
        //生成脚本
        String jarPath = getShell(outputPath, meta);
        //生成精简版的程序（只包含jar 脚本 源文件source）
        getDist(outputPath, jarPath, sourceRootDestPath,meta);
    }

    protected String getDist(String outputPath, String jarPath, String sourceRootDestPath,Meta meta) {
        //生成精简版的程序（只包含jar 脚本 源文件source）
        String distOutPutPath = outputPath +"-dist";
        String targetPath = distOutPutPath + File.separator + "target";
        FileUtil.mkdir(targetPath);
        //拷贝jar包
        String jarAbsolutePath = outputPath + File.separator + "generated" + File.separator + meta.getName() + File.separator + jarPath;
        FileUtil.copy(jarAbsolutePath,targetPath,true);
        //拷贝脚本
        String shellAbsolutePath = outputPath + File.separator + "generated" + File.separator + meta.getName()+ File.separator + "generate.bat";
        FileUtil.copy(shellAbsolutePath,distOutPutPath,true);
        //拷贝源文件
        FileUtil.copy(sourceRootDestPath, distOutPutPath,true);
        return distOutPutPath;
    }

    protected String getShell(String property, Meta meta) {
        //执行构建脚本得命令
        String outputShell = property + File.separator + "generated" + File.separator + meta.getName();
        String jarName = String.format("%s-%s-jar-with-dependencies.jar", meta.getName(), meta.getVersion());
        String jarPath = "target/" + jarName;
        ScripyGenerator.doGenerate(outputShell,jarPath);
        return jarPath;
    }

    protected void getJar(String property, Meta meta) throws IOException, InterruptedException {
        //执行构建jar包得命令
        String outputJar = property + File.separator + "generated"+File.separator+ meta.getName();
        JarGenerator.doGenerator(outputJar);
    }

    protected void doGeneratorCode(String outputPath, Meta meta) throws IOException, TemplateException {
        // 读取 resources 目录
        String inputResourcePath = "";
        // region 生成数据模型代码
        //构造输入文件路径
        String inputDynamicGeneratorFileDataModel = inputResourcePath + File.separator + "templates/java/model/DataModel.java.ftl";
        //构造输出文件路径
        String outputPathDataModel = outputPath + File.separator + "generated" + File.separator + meta.getName();
        if(!FileUtil.exist(outputPathDataModel)){
            FileUtil.mkdir(outputPathDataModel);
        }
        //构造基础包路径
        String metaBasePackage = meta.getBasePackage();
        String metaBasePackagePath = StrUtil.join("/", StrUtil.split(metaBasePackage, "."));
        String outputDynamicGeneratorFile = outputPathDataModel + File.separator + "src/main/java/"+ metaBasePackagePath;
        String outputDynamicGeneratorFileDataModel = outputDynamicGeneratorFile +"/model/DataModel.java";
        DynamicFileGenerator.doGenerator(inputDynamicGeneratorFileDataModel,outputDynamicGeneratorFileDataModel, meta);
        // endregion
        // region 生成命令代码
        /**
         * 构造输入命令文件路径
         */
        String inputDynamicGeneratorFileGenerateCommand = inputResourcePath + "/templates/java/cli/command/GenerateCommand.java.ftl";
        String inputDynamicGeneratorFileConfigCommand = inputResourcePath + "/templates/java/cli/command/ConfigCommand.java.ftl";
        String inputDynamicGeneratorFileJsonGenerateCommand = inputResourcePath + "/templates/java/cli/command/JsonGenerateCommand.java.ftl";
        String inputDynamicGeneratorFileListCommand = inputResourcePath + "/templates/java/cli/command/ListCommand.java.ftl";
        String inputDynamicGeneratorFileCommandExecutor = inputResourcePath + "/templates/java/cli/CommandExecutor.java.ftl";
        String inputDynamicGeneratorFileCommandMain = inputResourcePath + "/templates/java/Main.java.ftl";
        /**
         * 构造输出命令文件路径
         */
        String outputPathCli = outputDynamicGeneratorFile + File.separator + "cli";
        if(!FileUtil.exist(outputPathCli)){
            FileUtil.mkdir(outputPathCli);
        }
        String outputPathCommand = outputPathCli + File.separator + "command";
        if(!FileUtil.exist(outputPathCommand)){
            FileUtil.mkdir(outputPathCommand);
        }
        String outputDynamicGeneratorFileGenerateCommand = outputPathCommand + File.separator + "GenerateCommand.java";
        String outputDynamicGeneratorFileJsonGenerateCommand = outputPathCommand + File.separator + "JsonGenerateCommand.java";
        String outputDynamicGeneratorFileConfigCommand = outputPathCommand + File.separator + "ConfigCommand.java";
        String outputDynamicGeneratorFileListCommand = outputPathCommand + File.separator + "ListCommand.java";
        String outputDynamicGeneratorFileCommandExecutor = outputPathCli + File.separator + "CommandExecutor.java";
        String outputDynamicGeneratorFileMain = outputDynamicGeneratorFile + File.separator + "Main.java";
        DynamicFileGenerator.doGenerator(inputDynamicGeneratorFileGenerateCommand,outputDynamicGeneratorFileGenerateCommand, meta);
        DynamicFileGenerator.doGenerator(inputDynamicGeneratorFileJsonGenerateCommand,outputDynamicGeneratorFileJsonGenerateCommand, meta);
        DynamicFileGenerator.doGenerator(inputDynamicGeneratorFileConfigCommand,outputDynamicGeneratorFileConfigCommand, meta);
        DynamicFileGenerator.doGenerator(inputDynamicGeneratorFileListCommand,outputDynamicGeneratorFileListCommand, meta);
        DynamicFileGenerator.doGenerator(inputDynamicGeneratorFileCommandExecutor,outputDynamicGeneratorFileCommandExecutor, meta);
        DynamicFileGenerator.doGenerator(inputDynamicGeneratorFileCommandMain,outputDynamicGeneratorFileMain, meta);
        // endregion
        // region 生成生成文件代码
        String outputPathGenerate = outputDynamicGeneratorFile + File.separator + "generator";
        /**
         * 构建输入文件路径
         */
        if(!FileUtil.exist(outputPathGenerate)){
            FileUtil.mkdir(outputPathGenerate);
        }
        String inputDynamicFileGenerator = inputResourcePath + "/templates/java/generator/DynamicFileGenerator.java.ftl";
        String inputMainGenerator = inputResourcePath + "/templates/java/generator/MainGenerator.java.ftl";
        String inputStaticFileGenerator = inputResourcePath + "/templates/java/generator/StaticFileGenerator.java.ftl";
        /**
         * 构建输出文件路径
         */
        String outputDynamicFileGenerator = outputPathGenerate + File.separator + "DynamicFileGenerator.java";
        String outputMainGenerator = outputPathGenerate + File.separator + "MainGenerator.java";
        String outputStaticFileGenerator = outputPathGenerate + File.separator + "StaticFileGenerator.java";
        DynamicFileGenerator.doGenerator(inputDynamicFileGenerator,outputDynamicFileGenerator, meta);
        DynamicFileGenerator.doGenerator(inputMainGenerator,outputMainGenerator, meta);
        DynamicFileGenerator.doGenerator(inputStaticFileGenerator,outputStaticFileGenerator, meta);
        // endregion

        // 生成pom.xml文件
        // 构建pom.xml文件输入路径
        String inputPom = inputResourcePath + "/templates/pom.xml.ftl";
        // 构建pom.xml文件输出路径
        String outputPom = outputPath + File.separator + "generated" + File.separator + meta.getName()+File.separator+"pom.xml";
        DynamicFileGenerator.doGenerator(inputPom,outputPom, meta);

        // 生成README.md文件
        // 构建README.md文件输入路径
        String inputREAD = inputResourcePath + "/templates/README.md.ftl";
        // 构建README.md文件输出路径
        String outputREAD = inputResourcePath + File.separator + "generated" + File.separator + meta.getName()+File.separator+"README.md.ftl";
        DynamicFileGenerator.doGenerator(inputREAD,outputREAD, meta);
    }

    protected String getSourceRootDestPath(Meta meta, String property) {
        //        "sourceRootPath": "D:/Code-Generate/code-generate/lin-code-generate-maker/.temp/1/springboot-init-master",
        //D:/Code-Generate/code-generate/lin-code-generate-maker/generated/springboot-init-generator/source
        // region 复制原始文件
        String sourceRootPath = meta.getFileConfig().getSourceRootPath();//D:/Code-Generate/code-generate/lin-code-generate-maker/acm-template-pro
        String sourceRootDestPath = property + File.separator+"source";//generated\source
        FileUtil.copy(sourceRootPath, sourceRootDestPath, true);
        // endregion
        return sourceRootDestPath;
    }


    /**
     * 制作压缩包
     *
     * @param outputPath
     * @return 压缩包路径
     */
    protected String buildZip(String outputPath) {
        String zipPath = outputPath + ".zip";
        ZipUtil.zip(outputPath, zipPath);
        return zipPath;
    }
}
